import { ref } from 'vue';
import { FormSchema } from '/@/components/Form/index';
import { useI18n } from '/@/hooks/web/useI18n';

const { t } = useI18n();
const newPw = ref('');
/** 设置ROI密码 */
export const schemasPassword: FormSchema[] = [
  {
    field: 'pwd_new',
    component: 'InputPassword',
    label: t('密码') + ':',
    defaultValue: '',
    componentProps: {
      size: 'large',
      rows: 6,
      placeholder: t('请输入密码'),
      maxLength: 20,
      allowClear: false,
    },
    rules: [
      {
        required: true,
        validator: (rule, value) => {
          if (value) {
            newPw.value = value;
          }
          if (!value) {
            return Promise.reject(t('请输入密码'));
          }
          const regex = /^[a-zA-Z0-9]\w{5,19}$/;
          if (value.length < 6 || value.length > 20 || !regex.test(value)) {
            return Promise.reject(t('密码只能输入6-20位的数字、字母'));
          }
          return Promise.resolve();
        },
        trigger: 'blur',
      },
    ],
  },
  {
    field: 'pwd_confirm',
    component: 'InputPassword',
    label: t('确认密码') + ':',
    defaultValue: '',
    componentProps: {
      size: 'large',
      rows: 6,
      placeholder: t('请再次输入密码'),
      maxLength: 20,
      allowClear: false,
    },
    rules: [
      {
        required: true,
        validator: (rule, value) => {
          if (!value) {
            return Promise.reject(t('请再次输入确认密码'));
          }
          const regex = /^[a-zA-Z0-9]\w{5,19}$/;
          if (value.length < 6 || value.length > 20 || !regex.test(value)) {
            return Promise.reject(t('密码只能输入6-20位的数字、字母'));
          }
          if (value) {
            const newPwList = newPw.value.split('');
            const valueList = value.split('');
            for (let i = 0; i < newPwList.length; i++) {
              if (newPwList[i] !== valueList[i]) {
                return Promise.reject(t('密码与确认密码不一致'));
              }
            }
            if (newPwList.length != valueList.length)
              return Promise.reject(t('密码与确认密码不一致'));
          }
          return Promise.resolve();
        },
        trigger: 'blur',
      },
    ],
  },
];
